package com.ciwong.epaper.modules.me.ui;

import android.os.Handler;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.Gravity;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.ciwong.epaper.R;
import com.ciwong.epaper.application.EApplication;
import com.ciwong.epaper.modules.me.dao.MeDao;
import com.ciwong.epaper.util.BaseExtCallBack;
import com.ciwong.libs.utils.NetworkUtils;
import com.ciwong.mobilelib.application.BaseApplication;
import com.ciwong.mobilelib.i.ViewOnClickListener;
import com.ciwong.mobilelib.ui.BaseActivity;
import com.ciwong.mobilelib.utils.ToastUtil;

/**
 * Created by Administrator on 2017/6/13.
 * 意见反馈页面
 */

public class IssueRespActivity extends BaseActivity {
    private Button btn_yijian_idea;
    private EditText et_yijian;
    private ViewGroup contail;

    @Override
    protected int setView() {
        return R.layout.yijianfankui;
    }

    @Override
    protected void findViews() {
        btn_yijian_idea = (Button) findViewById(R.id.btn_submit_idea);
        et_yijian = (EditText) findViewById(R.id.et_yijian);
        contail = (ViewGroup) findViewById(R.id.contail);

    }


    private Handler mHandler = new Handler();

    @Override
    protected void initEvent() {
        btn_yijian_idea.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                hideSoftInput(v);
                return false;
            }
        });
        btn_yijian_idea.setOnClickListener(new ViewOnClickListener(5000l) {
            @Override
            public void avertRepeatOnClick(View v) {
                try {

                    if (!NetworkUtils.isOnline()) {
                        ToastUtil.INSTANCE.toastCenterNoNetError();
                        return;
                    }

                    String textCommit = et_yijian.getText().toString().trim();

                    if (textCommit.length() > 0) {
                        if (textCommit.equals("sendlog#")) {
                            BaseApplication.sendLog = true;
                            ToastUtil.INSTANCE.toastInCenter(IssueRespActivity.this, "日志已打开");
                            return;
                        } else if (textCommit.equals("closelog#")) {
                            BaseApplication.sendLog = false;
                            ToastUtil.INSTANCE.toastInCenter(IssueRespActivity.this, "日志已关闭");
                            return;
                        }
                        MeDao.getInstance().getIssued(IssueRespActivity.this, et_yijian.getText().toString(),
                                getUserInfoBase().getRealName(), getUserInfoBase().getMobile(),
                                new BaseExtCallBack(IssueRespActivity.this, getUserInfoBase().getUserId() + "") {
                                    @Override
                                    public void success(Object data) {
                                        MeDao.getInstance().getUserPoint();
                                        ToastUtil.INSTANCE.toastCenterSuccess("提交成功");
                                        mHandler.postDelayed(new Runnable() {
                                            @Override
                                            public void run() {
                                                finish();
                                            }
                                        }, 1000);

                                    }

                                    @Override
                                    public void failed(Object data) {
                                        super.failed(data);
                                        ToastUtil.INSTANCE.toastCenterError("提交失败");
                                    }

                                    @Override
                                    public void failed(int errorCode, Object data) {
                                        super.failed(errorCode, data);
                                        ToastUtil.INSTANCE.toastCenterErrorWithErrorCode("提交失败", errorCode);

                                    }
                                });
                    } else {
                        Toast toast = Toast.makeText(IssueRespActivity.this, "提交内容不能全为空格", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
                        toast.show();
                    }
                } catch (Exception e) {
                    e.getStackTrace();
                }

            }
        });
    }

    @Override
    protected void init() {
        setTitleText("意见反馈");
        et_yijian.addTextChangedListener(watcher);
    }

    @Override
    protected void loadData() {

    }

    private TextWatcher watcher = new TextWatcher() {

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            // TODO Auto-generated method stub

        }

        @Override
        public void beforeTextChanged(CharSequence s, int start, int count,
                                      int after) {
            // TODO Auto-generated method stub
            if (s.length() <= 0) {
                btn_yijian_idea.setEnabled(false);

            } else {
                btn_yijian_idea.setEnabled(true);
            }

        }

        @Override
        public void afterTextChanged(Editable s) {
            // TODO Auto-generated method stub
            if (s.length() <= 0) {
                btn_yijian_idea.setEnabled(false);

            } else {
                btn_yijian_idea.setEnabled(true);
            }
        }
    };
}
